﻿$(document).ready(function () {
    showData();
    $("#doing").click(function () {
        $("#doingthings").slideToggle(1000);
    });
    $("#done").click(function () {
        $("#donethings").slideToggle(1000);
    });
    $(".allclear").click(function () {
        $("li").fadeToggle(1000, function () {
            clearEvent();
        });
    });
})
//添加信息
function addaction() {
    var action = $("#action");
    if (action.val() == "") {
        alert("请添加事项!");
        return;
    }
    var event = { "action": action.val(), "isdone": false };
    var data = getData();
    data.push(event);
    saveData(data);
    showData();
    action.val("");
}
//删除
function deleteUser(index) {
    var data = getData();
    data.splice(index, 1);
    saveData(data);
    showData();
}
function getData() {
    var data = localStorage.getItem("todolist");
    if (data != null) {
        return JSON.parse(data);
    }
    else {
        return [];
    }
}
function saveData(data) {
    localStorage.setItem("todolist", JSON.stringify(data))
}

function showData(data) {
    var ol = $("#todolist");
    var ul = $("#donelist");
    var data = localStorage.getItem("todolist");
    if (data != null) {
        var todolist = JSON.parse(data);
        var numberOfTrue = 0, numberOfFalse = 0;
        var strTrue = "";
        var strFalse = "";
        for (var i = 0; i < todolist.length; i++) {
            if (todolist[i].isdone) {
                strTrue += "<li><input type=\"checkbox\" onclick=\"changeaction(" + i + ")\"/>" + todolist[i].action + "<span class=\"clearbutton\" id=\"clearbutton\" style=\"cursor:pointer\" onclick=\"deleteUser(" + i + ")\">-</span></li>";
                numberOfTrue++;
            }
            else {
                strFalse += "<li><input type=\"checkbox\" onclick=\"changeaction(" + i + ")\"/>" + todolist[i].action + "<span class=\"clearbutton\" id=\"clearbutton\" style=\"cursor:pointer\" onclick=\"deleteUser(" + i + ")\">-</span></li>";
                numberOfFalse++;
            }

        }
        ol.html(strFalse);
        ul.html(strTrue);
        $("#doingthings").html(numberOfFalse);
        $("#donethings").html(numberOfTrue);
    }
    else {
        ol.html("");
        ul.html("");
        $("#doingthings").html(0);
        $("#donethings").html(0);
    }
}
function changeaction(index) {
    var data = getData();
    if (data[index].isdone)
        data[index].isdone = false;
    else
        data[index].isdone = true;
    saveData(data);
    showData();
}
function clearEvent() {
    localStorage.clear();
    showData();
}